<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>编辑商品</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/app/admin/component/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" media="all">
    <style>
    .layui-form {padding: 10px 20px;}
    .layui-card {margin-bottom: 15px;}
    .layui-upload-img {max-width: 120px; margin: 10px 0;}
    .required:after {content: '*'; color: red; margin-left: 4px;}
    .help-block {color: #999; font-size: 12px; margin-top: 4px;}
    </style>
</head>
<body>
    <div class="layui-container">
        <form class="layui-form" lay-filter="form">
            <input type="hidden" name="id" value="<?= $goods->id ?>">
            <!-- 基本信息 -->
            <div class="layui-card">
                <div class="layui-card-header">基本信息</div>
                <div class="layui-card-body">
                    <div class="layui-form-item">
                        <label class="layui-form-label required">商品类型</label>
                        <div class="layui-input-block">
                            <input type="radio" name="type" value="1" title="普通商品" <?= $goods->type == 1 ? 'checked' : '' ?>>
                            <input type="radio" name="type" value="2" title="服务商品" <?= $goods->type == 2 ? 'checked' : '' ?>>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">商品编码</label>
                        <div class="layui-input-block">
                            <input type="text" name="code" value="<?= $goods->code ?>" lay-verify="required" placeholder="请输入商品编码" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">商品名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" value="<?= $goods->name ?>" lay-verify="required" placeholder="请输入商品名称" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">商品分类</label>
                        <div class="layui-input-block">
                            <select name="category_id" lay-verify="required">
                                <option value="">请选择商品分类</option>
                                <?php foreach ($categories as $category): ?>
                                    <option value="<?= $category->id ?>" <?= $category->id == $goods->category_id ? 'selected' : '' ?>><?= $category->name ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">规格</label>
                        <div class="layui-input-block">
                            <input type="text" name="spec" value="<?= $goods->spec ?? '' ?>" placeholder="请输入商品规格" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">单位</label>
                        <div class="layui-input-block">
                            <input type="text" name="unit" value="<?= $goods->unit ?? '' ?>" placeholder="请输入商品单位" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>

            <!-- 价格信息 -->
            <div class="layui-card">
                <div class="layui-card-header">价格信息</div>
                <div class="layui-card-body">
                    <div class="layui-form-item">
                        <label class="layui-form-label required">零售价格</label>
                        <div class="layui-input-block">
                            <input type="number" name="price" value="<?= $goods->price ?>" lay-verify="required|number" placeholder="请输入商品售价" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">会员价格</label>
                        <div class="layui-input-block">
                            <input type="number" name="member_price" value="<?= $goods->member_price ?: 0 ?>" placeholder="请输入会员价格" class="layui-input" step="0.01">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">参考进价</label>
                        <div class="layui-input-block">
                            <input type="number" name="original_price" value="<?= $goods->original_price ?? 0 ?>" placeholder="请输入参考进价" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>

            <!-- 库存信息 -->
            <div class="layui-card">
                <div class="layui-card-header">库存信息</div>
                <div class="layui-card-body">
                    <div class="layui-form-item">
                        <label class="layui-form-label required">库存数量</label>
                        <div class="layui-input-block">
                            <input type="number" name="stock" value="<?= $goods->stock ?>" lay-verify="required|number" placeholder="请输入库存数量" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">最低库存</label>
                        <div class="layui-input-block">
                            <input type="number" name="min_stock" value="<?= $goods->min_stock ?? 0 ?>" placeholder="请输入最低库存" class="layui-input">
                            <div class="help-block">低于此数量将提示补货</div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 其他信息 -->
            <div class="layui-card">
                <div class="layui-card-header">其他信息</div>
                <div class="layui-card-body">
                    <div class="layui-form-item">
                        <label class="layui-form-label">商品图片</label>
                        <div class="layui-input-block">
                            <div class="layui-upload">
                                <button type="button" class="layui-btn" id="upload-image">上传图片</button>
                                <div class="layui-upload-list">
                                    <img class="layui-upload-img" id="preview-image" src="<?= $goods->image ?>" style="max-width: 200px;">
                                    <p id="upload-text"></p>
                                </div>
                                <input type="hidden" name="image" id="image" value="<?= $goods->image ?>">
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label required">商品状态</label>
                        <div class="layui-input-block">
                            <input type="radio" name="status" value="1" title="上架" <?= $goods->status == 1 ? 'checked' : '' ?>>
                            <input type="radio" name="status" value="0" title="下架" <?= $goods->status == 0 ? 'checked' : '' ?>>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">积分设置</label>
                        <div class="layui-input-block">
                            <select name="points_rule" lay-filter="points_rule">
                                <option value="">不启用</option>
                                <option value="rate" <?= $goods->points_rule == 'rate' ? 'selected' : '' ?>>按比例</option>
                                <option value="fixed" <?= $goods->points_rule == 'fixed' ? 'selected' : '' ?>>固定积分</option>
                            </select>
                            <div class="layui-input-inline" id="points_value_container" style="display:none;">
                                <input type="number" name="points_value" value="<?= $goods->points_value ?? 0 ?>" placeholder="积分值" class="layui-input">
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">折扣设置</label>
                        <div class="layui-input-block">
                            <select name="discount_rule" lay-filter="discount_rule">
                                <option value="">不启用</option>
                                <option value="min" <?= $goods->discount_rule == 'min' ? 'selected' : '' ?>>最低折扣</option>
                                <option value="fixed" <?= $goods->discount_rule == 'fixed' ? 'selected' : '' ?>>固定折扣</option>
                            </select>
                            <div class="layui-input-inline" id="discount_value_container" style="display:none;">
                                <input type="number" name="discount_value" value="<?= $goods->discount_value ?? 0 ?>" placeholder="折扣值" class="layui-input">
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-block">
                            <textarea name="remark" placeholder="请输入备注" class="layui-textarea"><?= $goods->remark ?? '' ?></textarea>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="save">立即修改</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>

    <script src="/app/admin/component/layui/layui.js"></script>
    <script src="/app/admin/component/pear/pear.js"></script>
    <script>
    layui.use(['form', 'upload'], function(){
        var $ = layui.$
        ,form = layui.form
        ,upload = layui.upload;
        
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#upload-image'
            ,url: '/app/ky_shouyin/admin/upload/image'
            ,before: function(obj){
                obj.preview(function(index, file, result){
                    $('#preview-image').attr('src', result);
                });
            }
            ,done: function(res){
                if(res.code == 0){
                    $('#image').val(res.data.src);
                    $('#upload-text').html('');
                }else{
                    $('#upload-text').html('<span style="color: #FF5722;">上传失败: ' + res.msg + '</span>');
                }
            }
            ,error: function(){
                $('#upload-text').html('<span style="color: #FF5722;">上传失败</span>');
            }
        });
        
        // 初始化显示/隐藏积分和折扣输入框
        var initRuleContainers = function() {
            if($('select[name="points_rule"]').val()) {
                $('#points_value_container').show();
            }
            if($('select[name="discount_rule"]').val()) {
                $('#discount_value_container').show();
            }
        };
        initRuleContainers();
        
        // 监听积分规则选择
        form.on('select(points_rule)', function(data) {
            if(data.value) {
                $('#points_value_container').show();
            } else {
                $('#points_value_container').hide();
            }
        });
        
        // 监听折扣规则选择
        form.on('select(discount_rule)', function(data) {
            if(data.value) {
                $('#discount_value_container').show();
            } else {
                $('#discount_value_container').hide();
            }
        });
        
        // 监听提交
        form.on('submit(save)', function(data) {
            let field = data.field;
            // 确保所有数值字段都被正确处理
            field.price = parseFloat(field.price) || 0;
            field.member_price = parseFloat(field.member_price) || 0;
            field.original_price = parseFloat(field.original_price) || 0;
            field.stock = parseInt(field.stock) || 0;
            field.min_stock = parseInt(field.min_stock) || 0;
            field.points_value = parseFloat(field.points_value) || 0;
            field.discount_value = parseFloat(field.discount_value) || 0;
            // 确保规则字段有值
            field.points_rule = field.points_rule || '';
            field.discount_rule = field.discount_rule || '';
            
            $.ajax({
                url: '/app/ky_shouyin/admin/goods/edit'
                ,type: 'POST'
                ,data: field
                ,dataType: 'json'
                ,complete: function(xhr) {
                    console.log('提交的数据:', field);
                    console.log('服务器响应:', xhr.responseText);
                }
                ,success: function(res){
                    if(res.code === 0){
                        layer.msg('修改成功', {
                            icon: 1
                            ,time: 1000
                        }, function(){
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            parent.layui.table.reload('goods-table');
                        });
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }
                ,error: function(){
                    layer.msg('服务器错误，请稍后重试', {icon: 2});
                }
            });
            return false; // 阻止表单跳转
        });
    })
    </script>
</body>
</html> 